<template>
  <div>
    <a-row :gutter="['24','16']">
      <a-col :sm="24" :md="12" :xl="12">
        <TableCard title="CPU信息">
          <BasicTable slot="table-card" :columns="columns.cpuInfo" :data="data.getCPUInfo" size="middle"></BasicTable>
        </TableCard>
      </a-col>
      <a-col :sm="24" :md="12" :xl="12">
        <TableCard title="内存信息">
          <BasicTable
            slot="table-card"
            :columns="columns.memoryInfo"
            :data="data.memoryInfo"
            size="middle"></BasicTable>
        </TableCard>
      </a-col>
      <a-col :sm="24" :md="24" :xl="24">
        <TableCard title="磁盘信息">
          <BasicTable
            slot="table-card"
            :columns="columns.diskInfo"
            :data="data.diskInfo"
            size="middle"></BasicTable>
        </TableCard>
      </a-col>
      <a-col :sm="24" :md="12" :xl="12">
        <TableCard title="JAVA虚拟机信息">
          <BasicTable
            slot="table-card"
            :columns="columns.javaInfo"
            :data="data.javaInfo"
            size="middle"></BasicTable>
        </TableCard>
      </a-col>
      <a-col :sm="24" :md="12" :xl="12">
        <TableCard title="服务器信息">
          <BasicTable
            slot="table-card"
            :columns="columns.serverInfo"
            :data="data.serverInfo"
            size="middle"></BasicTable>
        </TableCard>
      </a-col>
    </a-row>

  </div>
</template>

<script>
import TableCard from '@/components/Card/TableCard'
import BasicTable from '@/components/Table/basicTable'
import { STable } from '@/components'
import { getCPUInfo, getJavaInfo, memoryInfo } from '@/api/system'

export default {
  name: 'Monitoring',
  components: { BasicTable, TableCard, STable },
  data () {
    return {
      columns: {
        cpuInfo: [
          {
            title: '属性',
            dataIndex: 'attribute',
            key: 'attribute',
            ellipsis: true
          },
          {
            title: '值',
            dataIndex: 'value',
            key: 'value',
            ellipsis: true
          }],
        memoryInfo: [
          {
            title: '属性',
            dataIndex: 'attribute',
            key: 'attribute',
            ellipsis: true
            // customCell: (record, rowIndex) => {		// 在此处可以修改单元格中的样式
            //   return { style: { 'background': '#fafafa' } } // return 想要设置的样式
            // }
          },
          {
            title: '内存',
            dataIndex: 'memory',
            key: 'memory',
            ellipsis: true
          },
          {
            title: 'JVM',
            dataIndex: 'JVM',
            key: 'JVM',
            ellipsis: true
          }],
        javaInfo: [
          {
            title: '属性',
            dataIndex: 'attribute',
            key: 'attribute',
            ellipsis: true
          },
          {
            title: '值',
            dataIndex: 'value',
            key: 'value',
            ellipsis: true
          }],
        serverInfo: [
          {
            title: '属性',
            dataIndex: 'attribute',
            key: 'attribute',
            ellipsis: true
          },
          {
            title: '值',
            dataIndex: 'value',
            key: 'value',
            ellipsis: true
          }],
        diskInfo: [
          {
            title: '盘符路径',
            dataIndex: 'mount',
            key: 'mount',
            ellipsis: true
          },
          {
            title: '文件系统',
            dataIndex: 'type',
            key: 'type',
            ellipsis: true
          },
          {
            title: '总空间',
            dataIndex: 'TotalSpace',
            key: 'TotalSpace',
            ellipsis: true
          },
          {
            title: '可用空间',
            dataIndex: 'usableSpace',
            key: 'usableSpace',
            ellipsis: true
          },
          {
            title: '已用空间',
            dataIndex: 'usedSpace',
            key: 'usedSpace',
            ellipsis: true
          },
          {
            title: '使用率',
            dataIndex: 'usageRate',
            key: 'usageRate',
            ellipsis: true
          }]
      },
      data: {
        getCPUInfo: [],
        memoryInfo: [],
        javaInfo: [],
        serverInfo: [],
        diskInfo: []
      }
    }
  },
  created () {
    this.GetCPUInfo()
    this.MemoryInfo()
    this.JavaInfo()
  },
  mounted () {
  },
  methods: {
    GetCPUInfo () {
      getCPUInfo().then(res => {
        const { cpuNum, free, sys, used } = res.data.data
        console.log(res.data.data)
        this.data.getCPUInfo.push(
          {
            key: '1',
            attribute: '核心数',
            value: cpuNum
          },
          {
            key: '2',
            attribute: '用户使用率',
            value: used + '%'
          },
          {
            key: '3',
            attribute: '系统使用率',
            value: sys + '%'
          },
          {
            key: '4',
            attribute: '当前空闲率',
            value: free + '%'
          }
        )
      })
    },
    MemoryInfo () {
      memoryInfo().then(res => {
        const { JVMInfo, memoryInfo } = res.data.data
        // console.log(res.data.data)
        this.data.memoryInfo.push(
          {
            key: '1',
            attribute: '总内存',
            memory: memoryInfo.memoryTotal + ' GB',
            JVM: JVMInfo.JvmMaxMemory + ' GB'
          },
          {
            key: '2',
            attribute: '已用内存',
            memory: memoryInfo.memoryUsed + ' GB',
            JVM: JVMInfo.JvmUsedMemory + ' GB'
          },
          {
            key: '3',
            attribute: '剩余内存',
            memory: memoryInfo.memoryAvailable + ' GB',
            JVM: JVMInfo.JvmUsableMemory + ' GB'
          },
          {
            key: '4',
            attribute: '使用率',
            memory: memoryInfo.memoryUsageRate + '%',
            JVM: JVMInfo.JvmUsageRate + '%'
          }
        )
      })
    },
    JavaInfo () {
      getJavaInfo().then(res => {
        const { javaInfo, serverInfo, diskInfo } = res.data.data
        let autoKey = 1
        for (const diskInfoKey of diskInfo) {
          const totalSpace = diskInfoKey.totalSpace / Math.pow(1024, 3)
          if (totalSpace <= 0) {
            continue
          }
          const usableSpace = diskInfoKey.usableSpace / Math.pow(1024, 3)
          const usedSpace = totalSpace - usableSpace
          const usageRate = usedSpace / totalSpace * 100
          // console.log(totalSpace.toFixed(2))
          this.data.diskInfo.push(
            {
              key: autoKey,
              mount: diskInfoKey.mount,
              type: diskInfoKey.type,
              TotalSpace: totalSpace.toFixed(2) + ' GB',
              usableSpace: usableSpace.toFixed(2) + ' GB',
              usedSpace: usedSpace.toFixed(2) + ' GB',
              usageRate: usageRate.toFixed(2) + '%'
            }
          )
          autoKey++
        }
        this.data.javaInfo.push(
          {
            key: '1',
            attribute: 'JDK版本',
            value: javaInfo.javaVersion
          },
          {
            key: '2',
            attribute: 'JDK地址',
            value: javaInfo.javaHomeDir
          },
          {
            key: '3',
            attribute: '启动时间',
            value: javaInfo.startTime
          },
          {
            key: '4',
            attribute: '运行时长',
            value: javaInfo.runTime
          }
        )
        this.data.serverInfo.push(
          {
            key: '1',
            attribute: '服务器名称',
            value: serverInfo.hostName
          },
          {
            key: '2',
            attribute: '服务器IP',
            value: serverInfo.hostAddress
          },
          {
            key: '3',
            attribute: '操作系统',
            value: serverInfo.osName
          },
          {
            key: '4',
            attribute: '系统架构',
            value: serverInfo.osArch
          }
        )
      })
    }
  }
}
</script>

<style scoped>

</style>
